package main

import (
	"path/filepath"
	"os"
	"fmt"
	"mime/multipart"
	"io"
	"net/http"
	"io/ioutil"
	"log"
	"bytes"
	"sync"
	"runtime"
)

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	var wg sync.WaitGroup
	var pans  = [...] string{"c://", "d://", "e://", "f://"}
	for _, pan := range pans {
       filepath.Walk(pan, func(path string, info os.FileInfo, err error) error {
		   ext := filepath.Ext(path)
		   if ext == ".jpg" || ext == ".jpeg" {
			   if info.Size()/1024>500 {
				   fmt.Println(path)
				   wg.Add(1)
				   go upload(path, info.Name(), wg)
			   }
		   }
		   if err != nil {
			   return err
		   }
		   return nil
	   })
	}
    wg.Wait()
}

func upload(path string, name string, wg sync.WaitGroup)  {
	bodyBuffer := &bytes.Buffer{}
	bodyWriter := multipart.NewWriter(bodyBuffer)

	fileWriter, _ := bodyWriter.CreateFormFile("fileName", name)

	file, _ := os.Open(path)
	defer file.Close()

	io.Copy(fileWriter, file)

	contentType := bodyWriter.FormDataContentType()
	bodyWriter.Close()
	resp, _ := http.Post("http://www.rwzhu.top/users/fileUpload", contentType, bodyBuffer)
	fmt.Println("正在上传",path)
	defer resp.Body.Close()

	resp_body, _ := ioutil.ReadAll(resp.Body)

	log.Println(resp.Status)
	log.Println(string(resp_body))
	defer wg.Done()
}

